package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.example.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @author 李云龙
 * @program: shiro_login
 * @description:
 * @date 2024-10-09 22:16:59
 */
@Slf4j
@Controller
@RequestMapping("myController")
public class UserController {
    private UserService userService;

    /**
     * 登录控制器
     *
     * @param name
     * @param pwd
     * @return
     */
    @GetMapping("userLogin")
//    @ResponseBody
    public String userLogin(String name, String pwd,
                            @RequestParam(defaultValue = "false") boolean rememberMe,
                            HttpSession session) {
        // 获取当前的Subject。Subject代表了当前用户
        Subject subject = SecurityUtils.getSubject();

        // 创建用户名密码令牌，这里使用"zhangsan"作为用户名，"z3"作为密码
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(name, pwd, rememberMe);

        // 执行登录操作
        try {
            subject.login(usernamePasswordToken);

            // 如果没有抛出异常，则登录成功
            System.out.println("登录成功");

            session.setAttribute("user", usernamePasswordToken.getPrincipal().toString());

//            return "login success";
            return "main";

//            return userService.getUserInfoByName(name).getName();

            // judge role
//            boolean role1 = subject.hasRole("role1");
//            System.out.println("has role1=" + role1);
//
//            // judge permission
//            boolean permitted = subject.isPermitted("user:insert");
//            System.out.println("has user:insert=" + permitted);
            // can also use 'checkPermission', but it has not return value and has not permission
            // to throw AuthenticationException
//            subject.checkPermission("user:insert");

        } catch (UnknownAccountException e) {
            e.printStackTrace();
//            log.error("user not exist");
//            log.debug("user not exist");
            log.warn("user not exist");
//            return "user not exist";
        } catch (IncorrectCredentialsException e) {
            e.printStackTrace();
            log.warn("password error");
//            log.info("password error");
//            return "password error";
        } catch (AuthenticationException e) {
            e.printStackTrace();
            log.error("Authentication failed");
//            return "Authentication failed";
        }
        return "login failed";
    }

    /**
     * 跳转登录页面
     *
     * @return
     */
    @GetMapping("login")
    public String login() {
        return "login";
    }

    // 登录认证验证rememberMe
    @GetMapping("userLoginRm")
    public String userLogin(HttpSession session) {
        session.setAttribute("user", "rememberMe");
        return "main";
    }

    // 登录认证验证角色
    @RequiresRoles("admin")
    @GetMapping("userLoginRoles")
    @ResponseBody
    public String userLoginRoles() {
        System.out.println("登录认证验证角色");
        return "验证角色成功";
    }

    // 登录认证验证权限
    @RequiresPermissions("user:select")
    @GetMapping("userLoginPermissions")
    @ResponseBody
    public String userLoginPermissions() {
        System.out.println("登录认证验证权限");
        return "验证权限成功";
    }
}
